<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="支付平台" prop="payType">
        <el-select v-model="queryParams.payType" placeholder="请选择支付平台" size="mini" clearable>
          <el-option :value="0" label="微信支付"/>
          <el-option :value="1" label="农商银联支付"/>
        </el-select>
      </el-form-item>
      <el-form-item label="订单状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="选择订单状态" clearable style="width:140px;" size="mini">
          <el-option label="未支付" :value="0" />
          <el-option label="待支付" :value="1" />
          <el-option label="已支付" :value="2" />
          <el-option label="已核销" :value="3" />
          <el-option label="已评价" :value="4" />
        </el-select>
      </el-form-item>
      <el-form-item label="订单号" prop="orderNo">
        <el-input v-model="queryParams.orderNo" placeholder="请输入订单号" clearable size="mini"
                  @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="查询时间" prop="propertyName">
        <el-date-picker v-model="dateRange" clearable size="mini" style="width: 240px" value-format="yyyy-MM-dd" type="daterange"
                        range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd"
                   v-hasPermi="['module:commonInfoOrder:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
                   v-hasPermi="['module:commonInfoOrder:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
                   v-hasPermi="['module:commonInfoOrder:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" icon="el-icon-download" size="mini" :loading="exportLoading" @click="handleExport"
                   v-hasPermi="['module:commonInfoOrder:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="orderList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="中介信息id" align="center" prop="infoId" />
      <el-table-column label="信息类型  0 - 房屋 1 - 车位" align="center" prop="infoType" />
      <el-table-column label="支付订单号" align="center" prop="orderNo" />
      <el-table-column label="流水号" align="center" prop="outTradeNo" />
<!--      <el-table-column label="发布者微信" align="center" prop="nickName" />-->
<!--      <el-table-column label="小区名称" align="center" prop="estateName" />-->
<!--      <el-table-column label="预留手机号" align="center" prop="phoneNum" />-->
      <el-table-column label="订单状态" align="center" prop="status" fixed="right">
        <template slot-scope="scope">
          <div>
            <!-- 订单状态 0 - 未支付   1 - 支付中  2 - 已支付  4  - 已发起退款  5 - 退款完成  6 - 退款失败  7 - 订单已完成（结束） 8 -  订单取消 -->
            <el-tag v-if="scope.row.state === 0" style="cursor: pointer" :type="'danger'">未支付</el-tag>
            <el-tag v-if="scope.row.state === 1" style="cursor: pointer" :type="''">待支付</el-tag>
            <el-tag v-if="scope.row.state === 2" style="cursor: pointer" :type="'success'">已支付</el-tag>
            <el-tag v-if="scope.row.state === 4" style="cursor: pointer" :type="'success'">已发起退款</el-tag>
            <el-tag v-if="scope.row.state === 5" style="cursor: pointer" :type="'warning'">退款完成</el-tag>
            <el-tag v-if="scope.row.state === 6" style="cursor: pointer" :type="'danger'">退款失败</el-tag>
            <el-tag v-if="scope.row.state === 7" style="cursor: pointer" :type="'danger'">订单已完成</el-tag>
            <el-tag v-if="scope.row.state === 8" style="cursor: pointer" :type="'danger'">订单取消</el-tag>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="总支付金额" align="center" prop="totalPrice" />
      <el-table-column label="支付总金额" align="center" prop="payPrice" />
      <el-table-column label="支付时间" align="center" prop="payTime" width="180"/>
      <el-table-column label="备注信息" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                     v-hasPermi="['module:commonInfoOrder:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                     v-hasPermi="['module:commonInfoOrder:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0"
                :total="total"
                :page.sync="queryParams.pageNum"
                :limit.sync="queryParams.pageSize"
                @pagination="getList" />

    <!-- 添加或修改跳蚤、易货商品的 订单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="中介信息id" prop="infoId">
          <el-input v-model="form.infoId" placeholder="请输入中介信息id" size="mini"/>
        </el-form-item>
        <el-form-item label="商家id" prop="merId">
          <el-input v-model="form.merId" placeholder="请输入商家id" size="mini"/>
        </el-form-item>
        <el-form-item label="发布者微信id" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入发布者微信id" size="mini"/>
        </el-form-item>
        <el-form-item label="所属小区id" prop="estateId">
          <el-input v-model="form.estateId" placeholder="请输入所属小区id" size="mini"/>
        </el-form-item>
        <el-form-item label="小区名称" prop="estateName">
          <el-input v-model="form.estateName" placeholder="请输入小区名称" size="mini"/>
        </el-form-item>
        <el-form-item label="第三方支付商户id" prop="payPlatformId">
          <el-input v-model="form.payPlatformId" placeholder="请输入第三方支付商户id" size="mini"/>
        </el-form-item>
        <el-form-item label="支付订单号" prop="orderNo">
          <el-input v-model="form.orderNo" placeholder="请输入支付订单号" size="mini"/>
        </el-form-item>
        <el-form-item label="微信、第三方支付订单号" prop="outTradeNo">
          <el-input v-model="form.outTradeNo" placeholder="请输入微信、第三方支付订单号" size="mini"/>
        </el-form-item>
        <el-form-item label="预留手机号" prop="phoneNum">
          <el-input v-model="form.phoneNum" placeholder="请输入预留手机号" size="mini"/>
        </el-form-item>
        <el-form-item label="支付状态" prop="payState">
          <el-input v-model="form.payState" placeholder="请输入支付状态" size="mini"/>
        </el-form-item>
        <el-form-item label="总支付金额" prop="totalPrice">
          <el-input v-model="form.totalPrice" placeholder="请输入总支付金额" size="mini"/>
        </el-form-item>
        <el-form-item label="备注信息" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注信息" size="mini"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" size="mini" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel" size="mini">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import { listOrder, getOrder, delOrder, addOrder, updateOrder, exportOrder } from "@/api/module/commonInfo/order";

  export default {
    name: "Order",
    data() {
      return {
        // 按钮loading
        buttonLoading: false,
        // 遮罩层
        loading: true,
        // 导出遮罩层
        exportLoading: false,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 跳蚤、易货商品的 订单表格数据
        orderList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
        }
      }
    },
    created() {
      this.getList();
    },
    methods: {
      /** 查询跳蚤、易货商品的 订单列表 */
      getList() {
        this.loading = true;
        listOrder(this.queryParams).then(response => {
          this.orderList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: undefined,
          infoId: undefined,
          infoType: undefined,
          merId: undefined,
          userId: undefined,
          estateId: undefined,
          estateName: undefined,
          payPlatform: undefined,
          payPlatformId: undefined,
          orderNo: undefined,
          outTradeNo: undefined,
          phoneNum: undefined,
          state: undefined,
          payState: undefined,
          totalPrice: undefined,
          payPrice: undefined,
          createTime: undefined,
          payTime: undefined,
          transactionId: undefined,
          refundOutTradeNo: undefined,
          refundMoney: undefined,
          refundState: undefined,
          refundResult: undefined,
          refundTransactionId: undefined,
          refundTime: undefined,
          propertySplitRate: undefined,
          propertySplitMoney: undefined,
          platformSplitRate: undefined,
          platformSplitMoney: undefined,
          propertyCashPayState: undefined,
          propertyCashPayStr: undefined,
          propertyCashRebackStr: undefined,
          remark: undefined,
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id);
        this.single = selection.length!==1;
        this.multiple = !selection.length;
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加跳蚤、易货商品的 订单";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.loading = true;
        this.reset();
        const id = row.id || this.ids;
        getOrder(id).then(response => {
          this.loading = false;
          this.form = response.data;
          this.open = true;
          this.title = "修改跳蚤、易货商品的 订单";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            this.buttonLoading = true;
            if (this.form.id != null) {
              updateOrder(this.form).then(response => {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }).finally(() => {
                this.buttonLoading = false;
              });
            } else {
              addOrder(this.form).then(response => {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }).finally(() => {
                this.buttonLoading = false;
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$confirm('是否确认删除跳蚤、易货商品的 订单编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.loading = true;
          return delOrder(ids);
        }).then(() => {
          this.loading = false;
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams;
        this.$confirm('是否确认导出所有跳蚤、易货商品的 订单数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportOrder(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
      }
    }
  };
</script>
